package admin.controllers;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import entities.Category;

import admin.models.CategoryModel;


public class DeleteCategoryServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		HttpSession session = request.getSession();
		response.setContentType("text/html");
		String path = ((HttpServletRequest) request).getRequestURI();
		String categoryId = "";
		try {
			categoryId = URLDecoder.decode(path.replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		} catch (Exception e) {
			response.sendRedirect("/admin/category/list");
		}
		try {
			CategoryModel categoryModel = CategoryModel.getInstance();
			Category category = categoryModel.getCategory(categoryId);
			categoryModel.delete(category);
			session.setAttribute("success", "Delete success category " + category.getName());
		} catch (Exception e) {
			// TODO: handle exception
		}
		response.sendRedirect("/admin/category/list");
	}
}
